﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Preactor.ObjectModelExtensions.Utilities
{
    using System.IO;
    using System.Windows.Forms;

    public static class FileUtilities
    {
        /// <summary>
        ///  Very simplistic for now. Need to do alot more error checking.
        /// </summary>
        /// <param name="from"></param>
        /// <param name="to"></param>
        /// <param name="fileNames"></param>
        /// <param name="overwrite"></param>
        /// <returns></returns>
        public static bool CopyFiles(string from, string to, IEnumerable<string> fileNames, bool overwrite = true )
        {
            try
            {
                if (!Directory.Exists(from))
                {
                    var warning = string.Format("The directory: {0}, does not exist. Cannot copy files.", from);
                    MessageBox.Show(warning, "Folder does not exist", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return false;
                }

                if (!Directory.Exists(to))
                {
                    Directory.CreateDirectory(to);
                }

                foreach (var fileName in fileNames)
                {
                    var fromFullPath = Path.Combine(from, fileName);

                    if (File.Exists(fromFullPath))
                    {
                        File.Copy(fromFullPath, Path.Combine(to, fileName), overwrite);
                    }
                    else
                    {
                        var warning = string.Format("The file: {0} does not exist in the folder: {1}. Will continue copying the other files.", fileName, from);
                        MessageBox.Show(warning, "File does not exist", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }
                }
            }
            catch (Exception)
            {
                var warning = string.Format("There was an error copying the files from : {0}, to: {1}.", from, to);
                MessageBox.Show(warning, "Folder does not exist", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return false;
            }

            return true;
        }
    }
}
